import com.aliyun.odps.udf.UDF;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpliderGoodsManualWeight extends UDF {


    /*
     * public static void main(String[] args) { String munual
     * ="产品规格有5ml、5ml/袋×15袋、200ml"; evaluate(munual); }
     */


    public String evaluate(String munual) {
        if (StringUtils.isNotBlank(munual)) {
            munual = munual.toLowerCase();
            //将括号处理掉
            if (munual.contains("(") || munual.contains("（")) {
                munual = munual.replace("(", " ");
                munual = munual.replace("（", " ");
            }
            if (munual.contains(")") || munual.contains("）")) {
                munual = munual.replace(")", "");
                munual = munual.replace("）", "");
            }
            /**
             * 使用正则匹提取出净含量和规格字段
             */
            munual = munual.replace(" ", "");
            munual = munual.replace("mi", "ml");
            System.out.println(munual);
            StringBuffer packingSb = new StringBuffer();
            List<String> regList = new ArrayList<String>();
            //regList.add();
            regList.add("\\d+包/袋|\\d+g/包|\\d+包");
            regList.add("\\d+.\\d+g\\*\\d+|\\d+ml\\*\\d+ml|\\d+.\\d+ml\\*\\d+|\\d+ml\\*\\d+.|\\d+ml×\\d+|\\d+ml\\*\\d+|\\d+g\\*\\d+.|\\d+nl" +
                    "\\d+g/片x\\d+片|\\d+mlx\\d+\\+\\d+.\\d+gx\\d+|\\d+.\\d+gx\\d+支|\\d+.\\d+g\\+\\d+ml/套|\\d+ml/\\d+.\\d+g|"
                    + "\\d+支\\*\\d+ml|\\d+mg/支|\\d+.\\dml/支|\\d+支"
                    + "\\d+.\\d+g|\\d+g/袋|\\d+ml/袋|\\d+袋/盒|\\d+g×\\d+袋/盒|\\d+g×\\d+支|\\d+g|\\d+.\\d+kg|\\d+ml×\\d+支|\\d+.\\d+ml|\\d+ml"
                    + "\\d+g/支|\\d+mg|\\d+mlx\\d+|\\d+.\\d+ml|\\d+.ml|\\d+ml|\\d+l|\\d+.\\d+l|\\d+.\\d+g|\\d+.g|\\d+g|\\d+kg|\\d+.\\d++oz/\\d+.\\d+g|\\d+.\\d+oz/\\d+g|"
                    + "\\d+.\\d+oz|\\d+oz|\\d+oz|\\d+.\\d+cm|\\d+cm|\\d+.\\d+fl|\\d+fl|\\d+千克|\\d+升");
            regList.add("\\d+\\*\\d+\\*\\d+mm|\\d+.\\d\\*\\d+|\\d+\\*\\d+.\\d+");
            regList.add("\\d+gx\\d+|\\d+g|\\d+片/盒|\\d+片|\\d+g/片");
            regList.add("\\d+ml×\\d+支|\\d+ml/支|\\d+ml/瓶|\\d+支/盒");
            regList.add("\\d+ml/片x\\d+片|\\d+ml/片\\*\\d+片|\\d+ml/片");
            //regList.add();
            regList.add("\\d+x\\d");
            regList.add("\\d+pcs|\\d+pc");
            regList.add("\\d+mg\\*\\d+粒/袋|\\d+粒x\\d.\\d+g/瓶|\\d+粒/瓶|\\d+粒");
            regList.add("\\d+g/瓶|\\d+瓶");
            regList.add("\\d+毫升");
            regList.add("\\d+ml/瓶");
            regList.add("\\d+片/盒|\\d+片|\\d+克");
            //regList.add("\\d+p");
            regList.add("\\d+件/套|\\d+条/盒");
            regList.add("\\d+贴/袋×\\d+袋|\\d+贴×\\d+袋|\\d+袋×\\d+贴|\\d+贴/盒|\\d+贴");
            regList.add("\\d+对/盒|//d+小包/袋|//d+对");
            regList.add("\\d+ｍl|１０ml|\\d+件套");
            regList.add("\\d+对装/盒|\\d+对装|\\d+对|\\d+个/盒|\\d+颗/盒\\d+枚/盒|\\d+只装|\\d+根/瓶|\\d+色");
            String packingWight = null;
            for (String reg : regList) {
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(munual);
                while (matcher.find()) {
                    System.out.println(reg);
                    String weight = matcher.group(0);
                    packingSb.append(weight).append(",");
                }
                if (packingSb.length() != 0 && packingSb.toString().contains(",")) {
                    packingWight = packingSb.substring(0, packingSb.lastIndexOf(","));
                    break;
                }

            }
            System.out.println(packingWight);
            return packingWight;
        }
        return null;
    }
}
